package org.vy.dal.dao;

import java.util.List;

import org.vy.dal.QueryManager;

public class VyMagazineSubscriptionDAO {
	private static final String FIND_BY_PK = "SELECT * FROM <TABLE> WHERE SUBSCRIPTION_ID = m_SubscriptionId";
	private static final String FIND_BY_PARAM = "SELECT * FROM <TABLE> WHERE <PARAM>";
	private static final String FIND_BY_VYID = "SELECT * FROM <TABLE> WHERE VY_ID = m_VyId AND IS_ACTIVE = TRUE";
	private static final String UPDATE = "UPDATE <TABLE> SET VY_ID = m_VyId, PATRIKA_ID = m_PatrikaId, PATRIKA_PERIOD_ID = m_PatrikaPeriodId, START_DATE = m_StartDate, END_DATE = m_EndDate, ADDRESS_ID = m_AddressId, DELIVERY_MODE = m_DeliveryMode, WEEK_OFFSET_INDEX = m_WeekOffsetIndex, RECEIPT_NO = m_ReceiptNo, RECEIPT_DATE = m_ReceiptDate, QUANTITY = m_Quantity, /*SHIPPING_AMOUNT = m_ShippingAmount, TOTAL_AMOUNT = m_TotalAmount, PAID_AMOUNT = m_PaidAmount,*/ REF_VY_ID = m_RefVyId, INSTRUCTION = m_Instruction, IS_ACTIVE = m_IsActive, LAST_UPDATED_BY = m_LastUpdatedBy, LAST_UPDATED_ON = m_LastUpdatedOn where SUBSCRIPTION_ID = m_SubscriptionId";
	private static final String INSERT = "INSERT INTO <TABLE> (SUBSCRIPTION_ID, VY_ID, PATRIKA_ID, PATRIKA_PERIOD_ID, START_DATE, END_DATE, ADDRESS_ID, DELIVERY_MODE, WEEK_OFFSET_INDEX, RECEIPT_NO, RECEIPT_DATE, QUANTITY, SHIPPING_AMOUNT, TOTAL_AMOUNT, PAID_AMOUNT, REF_VY_ID, INSTRUCTION, IS_ACTIVE, LAST_UPDATED_BY, LAST_UPDATED_ON) VALUES ( m_SubscriptionId, m_VyId, m_PatrikaId, m_PatrikaPeriodId, m_StartDate, m_EndDate, m_AddressId, m_DeliveryMode, m_WeekOffsetIndex, m_ReceiptNo, m_ReceiptDate, m_Quantity, m_ShippingAmount, m_TotalAmount, m_PaidAmount, m_RefVyId, m_Instruction, true, m_LastUpdatedBy, m_LastUpdatedOn )";
	private static final String DELETE = "UPDATE <TABLE> SET IS_ACTIVE = FALSE WHERE SUBSCRIPTION_ID = m_SubscriptionId";

	public static VyMagazineSubscriptionDO findByPK(String key)
			throws Exception {
		VyMagazineSubscriptionDO dto = new VyMagazineSubscriptionDO();
		dto.setSubscriptionId(key);
		dto = (VyMagazineSubscriptionDO) QueryManager.executeQuery(dto,
				FIND_BY_PK);
		return dto;
	}

	@SuppressWarnings("unchecked")
	public static List<VyMagazineSubscriptionDO> findActiveByVyId(String vyId)
			throws Exception {
		VyMagazineSubscriptionDO dto = new VyMagazineSubscriptionDO();
		dto.setVyId(vyId);
		return (List<VyMagazineSubscriptionDO>) QueryManager.executeListQuery(
				dto, FIND_BY_VYID);
	}

	@SuppressWarnings("unchecked")
	public static List<VyMagazineSubscriptionDO> findByParam(String where)
			throws Exception {
		VyMagazineSubscriptionDO dto = new VyMagazineSubscriptionDO();
		return (List<VyMagazineSubscriptionDO>) QueryManager.executeListQuery(
				dto, FIND_BY_PARAM.replaceAll("<PARAM>", where));
	}

	public static VyMagazineSubscriptionDO insert(VyMagazineSubscriptionDO dto)
			throws Exception {
		dto = (VyMagazineSubscriptionDO) QueryManager
				.executeInsert(dto, INSERT);
		return dto;
	}

	public static VyMagazineSubscriptionDO update(VyMagazineSubscriptionDO dto)
			throws Exception {
		int i = QueryManager.executeUpdate(dto, UPDATE);
		if (i > 0)
			return dto;
		else
			return null;
	}

	public static VyMagazineSubscriptionDO delete(String key) throws Exception {
		VyMagazineSubscriptionDO dto = new VyMagazineSubscriptionDO();
		dto.setSubscriptionId(key);
		boolean i = QueryManager.executeDelete(dto, DELETE);
		if (i == true)
			return dto;
		else
			return null;
	}

}
